package com.rd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcloud.cos.utils.Md5Utils;
import com.rd.mapper.AdminMapper;
import com.rd.pojo.Admin;
import com.rd.service.IAdminService;
import com.rd.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jianan
 * @since 2024-06-04
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    AdminMapper adminMapper;
    @Override
    public R login(String username, String password) {
        //1.根据用户名查询此用户信息
        Admin admin = adminMapper.selectByName(username);
        //2.判断admin
        if(admin ==null){
            return  R.fail().setMsg("登录失败,此账户不存在!");
        }
        //3.判断管理员账户的状态
        if(admin.getStatus() == 1){
            return  R.fail().setMsg("登录失败，此账号被封禁，请联系超级管理员!");
        }
        //4.判断密码
        String md5pwd = Md5Utils.md5Hex(password + admin.getSalt());//加密后的密码
        if(!md5pwd.equals(admin.getPassword())){
            return  R.fail().setMsg("登录失败，密码错误!");
        }
        //5.以上都满足，说明可以登录成功
        return R.ok().setMsg("恭喜登录成功!").setData(admin);
    }

}
